/**
 * 报告分析器
 * 实现组合模式，用于分析实验报告提交情况
 */
import logger from './Logger';

// 组件接口
class ReportComponent {
  constructor(name) {
    this.name = name;
  }

  add() {
    throw new Error('子类必须实现add方法');
  }

  remove() {
    throw new Error('子类必须实现remove方法');
  }

  getChild() {
    throw new Error('子类必须实现getChild方法');
  }

  getName() {
    return this.name;
  }

  analyze() {
    throw new Error('子类必须实现analyze方法');
  }
}

// 课程组件
class CourseComponent extends ReportComponent {
  constructor(name, id) {
    super(name);
    this.id = id;
    this.classes = [];
  }

  add(classComponent) {
    this.classes.push(classComponent);
  }

  remove(classComponent) {
    const index = this.classes.findIndex(c => c.id === classComponent.id);
    if (index !== -1) {
      this.classes.splice(index, 1);
    }
  }

  getChild(index) {
    return this.classes[index];
  }

  analyze(students, reportFiles) {
    logger.info(`分析课程 ${this.name} 的实验报告提交情况`);

    const result = {
      courseName: this.name,
      courseId: this.id,
      classes: []
    };

    for (const classComponent of this.classes) {
      const classResult = classComponent.analyze(students, reportFiles);
      result.classes.push(classResult);
    }

    return result;
  }
}

// 班级组件
class ClassComponent extends ReportComponent {
  constructor(name, id) {
    super(name);
    this.id = id;
    this.experiments = [];
  }

  add(experimentComponent) {
    this.experiments.push(experimentComponent);
  }

  remove(experimentComponent) {
    const index = this.experiments.findIndex(e => e.id === experimentComponent.id);
    if (index !== -1) {
      this.experiments.splice(index, 1);
    }
  }

  getChild(index) {
    return this.experiments[index];
  }

  analyze(students, reportFiles) {
    logger.info(`分析班级 ${this.name} 的实验报告提交情况`);

    // 过滤出该班级的学生
    const classStudents = students.filter(student => student.classId === this.id);

    const result = {
      className: this.name,
      classId: this.id,
      studentCount: classStudents.length,
      experiments: []
    };

    for (const experimentComponent of this.experiments) {
      const experimentResult = experimentComponent.analyze(classStudents, reportFiles);
      result.experiments.push(experimentResult);
    }

    return result;
  }
}

// 实验组件
class ExperimentComponent extends ReportComponent {
  constructor(name, id) {
    super(name);
    this.id = id;
  }

  add() {
    throw new Error('实验组件不能添加子组件');
  }

  remove() {
    throw new Error('实验组件不能移除子组件');
  }

  getChild() {
    throw new Error('实验组件没有子组件');
  }

  analyze(students, reportFiles) {
    logger.info(`分析实验 ${this.name} 的提交情况`);

    const result = {
      experimentName: this.name,
      experimentId: this.id,
      totalStudents: students.length,
      submittedCount: 0,
      missingCount: 0,
      missingStudents: []
    };

    // 检查每个学生是否提交了该实验的报告
    for (const student of students) {
      // 构建预期的文件名模式
      const expectedFilePattern = `${this.name}_${student.studentId}_${student.name}`;

      // 检查是否存在匹配的文件
      const hasSubmitted = reportFiles.some(file => {
        return file.name.includes(expectedFilePattern);
      });

      if (hasSubmitted) {
        result.submittedCount++;
      } else {
        result.missingCount++;
        result.missingStudents.push({
          studentId: student.studentId,
          name: student.name
        });
      }
    }

    // 计算提交率
    result.submissionRate = result.totalStudents > 0
      ? (result.submittedCount / result.totalStudents)
      : 0;

    return result;
  }
}

// 报告分析器
class ReportAnalyzer {
  constructor() {
    this.courses = [];
  }

  /**
   * 添加课程
   * @param {Object} course - 课程对象
   */
  addCourse(course) {
    const courseComponent = new CourseComponent(course.name, course.id);
    this.courses.push(courseComponent);
    return courseComponent;
  }

  /**
   * 添加班级到课程
   * @param {string} courseId - 课程ID
   * @param {Object} classObj - 班级对象
   */
  addClass(courseId, classObj) {
    const course = this.courses.find(c => c.id === courseId);
    if (!course) {
      throw new Error(`未找到ID为 ${courseId} 的课程`);
    }

    const classComponent = new ClassComponent(classObj.name, classObj.id);
    course.add(classComponent);
    return classComponent;
  }

  /**
   * 添加实验到班级
   * @param {string} courseId - 课程ID
   * @param {string} classId - 班级ID
   * @param {Object} experiment - 实验对象
   */
  addExperiment(courseId, classId, experiment) {
    const course = this.courses.find(c => c.id === courseId);
    if (!course) {
      throw new Error(`未找到ID为 ${courseId} 的课程`);
    }

    const classComponent = course.classes.find(c => c.id === classId);
    if (!classComponent) {
      throw new Error(`未找到ID为 ${classId} 的班级`);
    }

    const experimentComponent = new ExperimentComponent(experiment.name, experiment.id);
    classComponent.add(experimentComponent);
    return experimentComponent;
  }

  /**
   * 分析报告提交情况
   * @param {Array} students - 学生数组
   * @param {Array} reportFiles - 报告文件数组
   * @returns {Object} 分析结果
   */
  analyzeReports(students, reportFiles) {
    logger.info('开始分析实验报告提交情况');

    const results = [];

    for (const course of this.courses) {
      const courseResult = course.analyze(students, reportFiles);
      results.push(courseResult);
    }

    return results;
  }
}

export default ReportAnalyzer;
